package com.example.android.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ListView
import com.example.android.adapters.PayListViewAdapter
import com.example.android.models.Pay
import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.example.android.R
import com.example.android.activities.LoginActivity
import com.example.android.activities.MainActivity






// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 模块：缴纳学费
 * 功能：显示缴费清单，点击button实现付款跳转
 * 彭燕楠
 */

class OnlinePaymentFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    private var pay: Button? =null
    private lateinit var adapter:PayListViewAdapter
    private var data = ArrayList<Pay>()
    var listView: ListView? = null
    var f = PayDetailFragment()

//    override fun onClick(v: View?) {
//        when(v?.id){
//            R.id.paybutton ->
//                getActivity()?.supportFragmentManager
//                ?.beginTransaction()
//                ?.replace(R.id.content_main_fragment,f)
//                ?.addToBackStack(null)
//                ?.commit();
//        }
//
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_online_payment, container, false)
   //     val page: View = inflater.inflate(R.layout.fragment_online_payment, container, false)
        //显示缴费项目


//        var pay = findViewById<Button>(R.id.paybutton)
//
//    //    var pay = view?.findViewById<View>(R.id.paybutton)
//        Log.d(toString(),"pay = ${pay}")
//        var f = PayDetailFragment()
//        pay?.setOnClickListener{
//            getActivity()?.supportFragmentManager
//                ?.beginTransaction()
//                ?.replace(R.id.content_main_fragment,f)
//                ?.addToBackStack(null)
//                ?.commit();
//        }


        //点击button跳转支付界面
//        button!!.setOnClickListener {
////            getActivity()?.getSupportFragmentManager()
////                ?.beginTransaction()
////                .replace(R.id.content_main_fragment,PayDetailFragment)
////                .addToBackStack(null)
////                .commit();
//            val f2 = PayDetailFragment()
//            getFragmentManager()?.beginTransaction()?.replace(com.example.android.R.id.content_main_fragment, f2)?.commit();
//        }

    }

    //缴费清单页面，点击button实现付款跳转

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        pay = view.findViewById(R.id.paybutton)
        pay?.setOnClickListener {
            val PayFragment = PayDetailFragment()
            requireActivity().supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(
                    R.anim.fragment_item_select_in,
                    R.anim.fragment_item_select_out,
                    R.anim.fragment_item_back_in,
                    R.anim.fragment_item_back_out)
                .replace(R.id.content_main_fragment, PayFragment)
                .addToBackStack(null)
                .commit()
        }

        listView = view?.findViewById(R.id.paylistview)
        initData()
        var adapter = getActivity()?.let { PayListViewAdapter(it,data) }
        listView!!.adapter = adapter


    }


    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment OnlinePaymentFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            OnlinePaymentFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
    //载入数据
    private fun initData() {
        repeat(1) {
            data.add(Pay("住宿费","¥1500/年", com.example.android.R.drawable.accommodationfee))
            data.add(Pay("优质被套","¥240", com.example.android.R.drawable.bed))
            data.add(Pay("书本费", "¥300", com.example.android.R.drawable.book))
            data.add(Pay("学费", "¥5400/学年", com.example.android.R.drawable.school))
            data.add(Pay("医疗保险", "¥300/学期", com.example.android.R.drawable.medicial))
            data.add(Pay("空调租赁","¥1200/四学年", com.example.android.R.drawable.aircondition))
            data.add(Pay("军训费", "¥200", com.example.android.R.drawable.arm))
            data.add(Pay("军训服装费", "¥200", com.example.android.R.drawable.close))
            data.add(Pay("军训伙食费", "¥300", com.example.android.R.drawable.food))
        }
    }

}

